package freaks.terah.gui;

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;

import javax.swing.border.LineBorder;

import freaks.terah.model.FileIO;

import java.awt.Color;

public class IntroFrame extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JRadioButton rdbtnClient;
	private JRadioButton rdbtnServer;
	private JButton btnNewGame;
	private JButton btnContinueGame;
	private JLabel lblYourRole;
	private Gui guiParent;
	private JPanel thisPanel;
	private int tempRole;
	/**
	 * Create the panel.
	 */
	public IntroFrame(Gui parent) {
		setLayout(null);
		thisPanel = this;
		guiParent = parent;
		
		btnNewGame = new JButton("New Game");
		btnNewGame.setBounds(151, 93, 141, 29);
		btnNewGame.addActionListener(this);
		add(btnNewGame);
		
		btnContinueGame = new JButton("Continue Game");
		btnContinueGame.setBounds(151, 122, 141, 29);
		btnContinueGame.addActionListener(this);
		add(btnContinueGame);
		
		JLabel lblNewLabel = new JLabel("Start a game");
		lblNewLabel.setBounds(110, 17, 90, 34);
		add(lblNewLabel);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel.setBounds(22, 63, 100, 100);
		add(panel);
		panel.setLayout(null);
		
		rdbtnClient = new JRadioButton("Client");
		rdbtnClient.setBounds(6, 65, 81, 23);
		panel.add(rdbtnClient);
		rdbtnClient.addActionListener(this);
		rdbtnClient.setSelected(true);
		tempRole = 1;
		
		rdbtnServer = new JRadioButton("Server");
		rdbtnServer.setBounds(6, 34, 81, 23);
		panel.add(rdbtnServer);
		rdbtnServer.addActionListener(this);
		
		lblYourRole = new JLabel("Your role");
		lblYourRole.setBounds(20, 6, 61, 16);
		panel.add(lblYourRole);
		rdbtnServer.addActionListener(this);
		
		ButtonGroup btnGroup = new ButtonGroup();
		btnGroup.add(rdbtnClient);
		btnGroup.add(rdbtnServer);
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == rdbtnServer)
		{
			lblYourRole.setText("Server");
			tempRole = 2;
		}
		if (arg0.getSource() == rdbtnClient)
		{
			lblYourRole.setText("Client");
			tempRole = 1;
		}
		if (arg0.getSource() == btnNewGame)
		{
			guiParent.setNewFrame(tempRole);
		}
		if (arg0.getSource() == btnContinueGame)
		{
			FileIO io = new FileIO();
			
			File chosenFile = io.getSaveFile(thisPanel);
			
			if (chosenFile != null)
			{
				try {
				      int len;
				      char[] chr = new char[4096];
				      final StringBuffer buffer = new StringBuffer();
				      final FileReader reader = new FileReader(chosenFile);
				      try {
				          while ((len = reader.read(chr)) > 0) {
				              buffer.append(chr, 0, len);
				          }
				      } finally {
				          reader.close();
				      }
				      
				      if (tempRole == 1)
				      {
				    	  String string = JOptionPane.showInputDialog(this, "Enter the server string for this turn!");

				    	  if (string != null)
				    	  {
				    		  guiParent.setMainFrame(buffer.toString(),string);
				    		  return ;
				          }
				      }   
				      
				      if (tempRole == 2)
				      {
				    	  guiParent.setMainFrame(buffer.toString(), null);
				    	  return ;
				      }
				      
				      JOptionPane.showMessageDialog(this, "Invalid server message", "Error", JOptionPane.ERROR_MESSAGE);
				      
				} catch (Exception e) {
					  e.printStackTrace();
					  JOptionPane.showMessageDialog(this, "There was an error while loading the file", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}
}
